package com.sunday.volunteer.admin.adapter.index;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.entity.MyActivities;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/11/1.
 * 游玩需求
 */
public class Tab1RecycleAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<MyActivities> activitiesList;
    private int width;

    public Tab1RecycleAdapter(Context context,List<MyActivities> activitiesList) {
        this.context = context;
        width = DeviceUtils.getDisplay(context).widthPixels;
        this.activitiesList = activitiesList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_tab1_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holder1 = (ViewHolder) holder;
        final MyActivities activities = activitiesList.get(position);
        if (!TextUtils.isEmpty(activities.getMainImage())){
            Picasso.with(context)
                    .load(activities.getMainImage())
                    .resize(220,220)
                    .placeholder(R.drawable.default_error)
                    .error(R.drawable.default_error)
                    .into(holder1.img_main);
        }
        holder1.tvAddress.setText(activities.getAddress());
        holder1.tvTitle.setText(activities.getName());
        holder1.tvDate.setText(activities.getTime());
        holder1.tvNum.setText(activities.getLimitNumber()+"人");
        if (activities.getMembers().size()==0){
            holder1.l_entry.setVisibility(View.GONE);
        }else {
            holder1.l_entry.setVisibility(View.VISIBLE);
            Tab1EntryAdapter entryAdapter = new Tab1EntryAdapter(context,activities.getMembers());
            final LinearLayoutManager manager = new LinearLayoutManager(context);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            holder1.recyclerView.setLayoutManager(manager);
            holder1.recyclerView.setAdapter(entryAdapter);
            holder1.recyclerView.setFocusable(false);
        }
        holder1.lMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener!=null){
                    itemClickListener.itemClick(v,activities.getId());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return activitiesList.size();
    }

    public interface ItemClickListener {
        void itemClick(View v, long id);
    }

    private ItemClickListener itemClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.tv_address)
        TextView tvAddress;
        @Bind(R.id.tv_num)
        TextView tvNum;
        @Bind(R.id.recycler_view)
        RecyclerView recyclerView;
        @Bind(R.id.l_main)
        RelativeLayout lMain;
        @Bind(R.id.img_main)
        ImageView img_main;
        @Bind(R.id.l_entry)
        View l_entry;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
